package com.dayuanit.dy15.lesson20.demo3;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        Dog dog1 = new Dog("wc001", 100);
        Dog dog2 = new Dog("wc001", 100);

//        System.out.println(dog1 == dog2);
        System.out.println(dog1.equals(dog2));

        //图书馆
        System.out.println(dog1.hashCode());
        System.out.println(dog2.hashCode());

        //规则：
        //1.重写equals方法，hashcode方法也要重写
        //2.equals方法如果是true,那么hashcode值也要是相等
        //3.hashcode值相等，equals未必相等。


        System.out.println("---------------------");
        //哈希
        HashSet hashSet = new HashSet();
        hashSet.add("tom");
        hashSet.add("bob");//
        hashSet.add("ali");
        hashSet.add("cat");
        hashSet.add("bob");//

        //Hashset是不重复的 且 是无序的
        for (Object object : hashSet) {
            System.out.println(object);
        }

        System.out.println("----------");

        //哈希 也称之为 散列
        HashSet hashSet2 = new HashSet();
        hashSet2.add(new Dog("wc001", 100));
        hashSet2.add(new Dog("wc002", 100));
        hashSet2.add(new Dog("wc001", 200));
        hashSet2.add(new Dog("wc001", 200));

        //如何判断元素不重复？
        //1.根据hash值来确定元素所在区域
        //2.在当前区域遍历所有元素，如果有和当前元素相等的，则覆盖掉。

        System.out.println(hashSet2.size());


        //Map集合key-value，也就是键值对的方式，存储数据的。
        HashMap hashMap = new HashMap();
        hashMap.put("wc001", new Dog("wc001", 100));
        hashMap.put("wc002", new Dog("wc002", 100));
        hashMap.put("wc003", new Dog("wc003", 100));

        Dog dog = (Dog)hashMap.get("wc001");
        System.out.println(dog.getName() + " " + dog.getPrice());
        System.out.println(dog);
        System.out.println(dog.toString());

        System.out.println("---------------");

        //火星车
        List list = new ArrayList();
        list.add(100);
        list.add(true);
        list.add("tom");
        list.add(new Dog("wc001", 200));

        for (int i=0; i<list.size(); i++) {
            System.out.println(list.get(i));
        }

        for (Object object : list) {
            System.out.println(object);
        }

        System.out.println("-------");

        // ****** Dog ******
        List<Dog> dogList = new ArrayList();
        dogList.add(new Dog("wc001", 100));
        dogList.add(new Dog("wc002", 200));
//        dogList.add("tom");//error

        for (Dog dogdog : dogList) {
            System.out.println(dogdog);
        }

        System.out.println("------------");

        //泛型  编译期做类型检查
        List<String> strList = new ArrayList<>();
        strList.add("tom");
//        strList.add(200);
//        strList.add(new Dog("xx", 200));

    }
}
